home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 …ember: Reference Library / Apple Developer Reference Library (December 1999) (Disk 1).iso / pc / technical documentation / develop / develop issue 29 / develop issue 29 code / newton web server / nhttpd Ä / inet constants < prev    next >
Encoding:
Text File  |  1996-06-17  |  5.4 KB  |  281 lines

  1. // Constants for using the Newton Internet Enabler
  2. // Copyright (c) 1994-1995 Apple Computer, Inc.  All rights reserved.
  3.  
  4.  
  5. // Transport service type constants
  6. constant kTCP := 1;
  7. constant kUDP := 2;
  8.  
  9.  
  10. // Utility functions for Internet applications
  11.  
  12. DefConst('kNumtoHostAddr,func(addr)
  13. begin
  14.     // This is the inverse of HostAddrToNum
  15.     // It converts a 4 byte array to a "w.x.y.z" string.
  16.     local index, str := "";
  17.  
  18.     // A sanity check
  19.     if Length(addr) <> 4 then begin
  20.         return nil;
  21.     end;
  22.  
  23.     for index := 0 to Length(addr) - 1 do begin
  24.         str := str & NumberStr(addr[index]);
  25.         if index < Length(addr) - 1 then
  26.             str := str & ".";
  27.     end;
  28.  
  29.     return str;
  30. end);
  31.  
  32. DefConst('kHostAddrToNum,func(str)
  33. begin
  34.     // This is the inverse of NumToHostAddr
  35.     // It converts a "w.x.y.z" string to a 4 byte array.
  36.     // Actually the separators between the numbers can be any non-digits, except space
  37.     local index := 0, partCnt := 0;
  38.     local subNum := 0, digits := "0123456789 ";
  39.     local num := [0,0,0,0], processNum;
  40.  
  41.     for index := 0 to StrLen(str) - 1 do begin
  42.         ch := StrPos(digits, SubStr(str, index, 1), 0);
  43.         if not ch then begin
  44.             // If first character is not a digit then skip junk at beginning
  45.             if index = 0 then processNum := nil;
  46.  
  47.             // Not a digit.  Process number, unless skipping junk
  48.             if processNum then begin
  49.                 // Truncate numbers greater than 255
  50.                 if subNum > 255 then subNum := 255;
  51.                 if partCnt < 4 then
  52.                     num[partCnt] := subNum;
  53.                 partCnt := partCnt + 1;
  54.                 subNum := 0;
  55.                 processNum := nil;
  56.             end;
  57.         end else begin
  58.             // Add the next digit to subNum (unless it was a space)
  59.             if ch < 10 then
  60.                 subNum := subNum * 10 + ch;
  61.             processNum := true;
  62.         end;
  63.     end;
  64.  
  65.     // Truncate numbers greater than 255
  66.     if subNum > 255 then subNum := 255;
  67.  
  68.     // Do the last part (which usually won't have a trailing .)
  69.     if subNum <> 0 and partCnt < 4 then
  70.         num[partCnt] := subNum;
  71.  
  72.     return num;
  73. end);
  74.  
  75.  
  76. DefConst('kIsIPAddr,func(str)
  77. begin
  78.     // This returns true if the string is an IP address (as opposed to a domain name)
  79.     // cheap test now just tests if the string ends with a numeric character
  80.     local digits := "0123456789";
  81.     local len := strLen(str);
  82.     if len > 0 and  StrPos(digits, SubStr(str, len - 1, 1), 0) then
  83.         true
  84.     else 
  85.         nil;
  86. end);
  87.  
  88.  
  89. DefConst('kPortAddrStruct, [
  90.         'struct,
  91.         ['array,'byte,4],
  92.         'short
  93.     ]);    
  94.  
  95.  
  96. DefConst('kPortAddrData,{
  97.     arglist:    
  98.     [
  99.         [0,0,0,0],
  100.         0,    // destination port number    
  101.                 
  102.     ],
  103.     typelist: kPortAddrStruct,
  104. });
  105.  
  106.  
  107. // Use this to get Instantiate options
  108. // protocol must be kSLIP or kPPP
  109. DefConst('kGetEndpointConfigOptions, func(linkID,protocol) 
  110.     return [
  111.         {    
  112.             label:    "inet",
  113.             type:        'service,
  114.             opCode:    opSetRequired,
  115.             result:    nil,    
  116.         },
  117.         {    
  118.             label:    "ilid",            // set the link id
  119.             type:        'option,
  120.             opCode:    opSetRequired,
  121.             result:    nil,
  122.             form:        'template,
  123.             data:    
  124.             {
  125.                 arglist:    
  126.                 [
  127.                     linkID
  128.                 ],
  129.                 typelist:
  130.                 [
  131.                     'struct,
  132.                     'ulong
  133.                 ],    
  134.             },
  135.         },
  136.         {    
  137.             label:    "itsv",            // set the transport protocol (TCP or UCP)
  138.             type:        'option,
  139.             opCode:    opSetRequired,
  140.             result:    nil,
  141.             form:        'template,
  142.             data:    
  143.             {
  144.                 arglist:    
  145.                 [
  146.                     protocol    
  147.                 ],
  148.                 typelist:
  149.                 [
  150.                     'struct,
  151.                     'ulong
  152.                 ],    
  153.             },
  154.         },
  155.     ]);
  156.  
  157.  
  158.  
  159. DefConst('kUDPReceiveOptions,    [        
  160.         {    
  161.             label:    "iuds",            // get the UDP destination socket
  162.             type:        'option,
  163.             opCode:    opGetCurrent,
  164.             result:    nil,
  165.             form:        'template,
  166.             data:     kPortAddrData,    
  167.         },
  168.         {    
  169.             label:    "iuss",            // get the UDP source socket
  170.             type:        'option,
  171.             opCode:    opGetCurrent,
  172.             result:    nil,
  173.             form:        'template,
  174.             data:        kPortAddrData,
  175.         },
  176.     ]);
  177.     
  178.     
  179.  
  180. DefConst('kINetBindOptions,func(localPort,useDefaultPort) begin
  181.         return [
  182.             {    
  183.                 label: "ilpt",                // set the local port
  184.                 type: 'option,
  185.                 opCode: opSetRequired,
  186.                 result: nil,
  187.                 form: 'template,
  188.                 data:    
  189.                 {
  190.                     arglist:    
  191.                     [
  192.                         localPort,            // local port number
  193.                         useDefaultPort    // use default port    
  194.                                 
  195.                     ],
  196.                     typelist:
  197.                     [
  198.                         'struct,
  199.                         'short,
  200.                         'boolean
  201.                     ],    
  202.                 },
  203.             },
  204.         ];
  205. end);
  206.  
  207. DefConst('kTCPConnectOptions, func(remoteAddr,remotePort) begin
  208.             return [
  209.                 {    
  210.                     label:    "itrs",            // set the TCP remote socket
  211.                     type:        'option,
  212.                     opCode:    opSetRequired,
  213.                     result:    nil,
  214.                     form:        'template,
  215.                     data:    
  216.                     {
  217.                         arglist:    
  218.                         [
  219.                             remoteAddr,
  220.                             remotePort,        // remote port number    
  221.                                     
  222.                         ],
  223.                         typelist: kPortAddrStruct,
  224.                     },
  225.                 },
  226.             ];
  227.     end);
  228.             
  229. DefConst('kTCPListenOptions,
  230.             [
  231.                 {    
  232.                     label:    "itrs",            // get the TCP remote socket
  233.                     type:        'option,
  234.                     opCode:    opGetCurrent,
  235.                     result:    nil,
  236.                     form:        'template,
  237.                     data:    kPortAddrData,
  238.                 },
  239.             ];
  240.     );
  241.             
  242. DefConst('kGetIPAddressesOptions,
  243.     [{
  244.          type    : 'option,
  245.          label   : "iprf",
  246.          opCode: opGetCurrent,
  247.          data    : {
  248.                   arglist: [
  249.                           [0,0,0,0],
  250.                           [0,0,0,0],
  251.                    ],
  252.                    typelist: [
  253.                            'struct,
  254.                            ['array,'byte,4],
  255.                            ['array,'byte,4],
  256.                    ]
  257.            }
  258.      }]);
  259.         
  260.  
  261. DefConst('kUDPPutBytesOptions,func(addr,port) begin
  262.         return    [{    
  263.                 label:    "iuds",            // set the UDP destination socket
  264.                 type:        'option,
  265.                 opCode:    opSetRequired,
  266.                 result:    nil,
  267.                 form:        'template,
  268.                 data:    
  269.                 {
  270.                     arglist:    
  271.                     [
  272.                         addr,
  273.                         port,        // remote port number    
  274.                                 
  275.                     ],
  276.                     typelist: kPortAddrStruct,
  277.                 },
  278.             }];
  279.     end);        
  280.  
  281.